//
//  HSLeftViewController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/27.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit
import SnapKit

class HSLeftViewController: UIViewController {
    
    //头像
    @IBOutlet weak var leftIconView: UIImageView!
    //用户名称
    @IBOutlet weak var leftNameL: UILabel!
    //班级名
    @IBOutlet weak var leftClassNameL: UILabel!
    //学校名
    @IBOutlet weak var leftSchoolNameL: UILabel!
    //账号名
    @IBOutlet weak var leftAccountL: UILabel!
    //表现分
    @IBOutlet weak var leftScoreL: UILabel!
    //表现分数分级视图
    @IBOutlet weak var leftScoreBgView: UIView!
    //头像宽度
    @IBOutlet weak var leftIconConstraint: NSLayoutConstraint!
    //
    var imageV = UIImageView()
    
    ///左边按钮数组
    @IBOutlet var leftBtnArray: [UIButton]!
    
    //MARK: - 按钮的监听回调方法
    @IBAction func leftBtnClicked(_ sender: UIButton) {
        switch sender.currentTitle! {
        case "课程表":
            present(HSClassTableController(), animated: true, completion: nil)
        case "关于我们":
            
            let aboutWeVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "aboutWe")
            present(aboutWeVc, animated: true, completion: nil)
        case "意见反馈":
            present(UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "feedBack"), animated: true, completion: nil)
        default:
            //弹窗确认是否退出
            let alertVc = UIAlertController(title: "确定要退出吗?", message: "", preferredStyle: .alert)
            let sureAc = UIAlertAction(title: "确定", style: .default, handler: { (_) in
                //获得登录控制器
                let loginVc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSLoginController")
                UIApplication.shared.keyWindow?.rootViewController = loginVc
                //修改登录状态
                UserDefaults.standard.set(false, forKey: isLogin)
            })
            let cencel = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            alertVc.addAction(cencel)
            alertVc.addAction(sureAc)
            present(alertVc, animated: true, completion: nil)
            
        }
    }
    
    //MARK: - UI设置
    override func viewDidLoad() {
        super.viewDidLoad()

         setupUI()
    }
    fileprivate func setupUI(){
        //设置背景
         view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        //设置头像大小
        leftIconConstraint.constant = screenW <= 320 ? 60 : 80
        //设置头像圆角
        leftIconView.layer.cornerRadius = leftIconConstraint.constant/2
        //裁剪
        leftIconView.layer.masksToBounds = true
        //设置边框
        leftIconView.layer.borderWidth = 2
        leftIconView.layer.borderColor = UIColor.white.cgColor
        
        //设置左边按钮的高亮背景色
        for btn in leftBtnArray{
            //先获得图片
            let image = UIImage.lhy_colorChangeImage(color: UIColor().colorWithHex(0xD0D0D0), size: btn.bounds.size)
        btn.setBackgroundImage(image, for: .highlighted)
        }
        
        //获得保存的用户信息数据 --通过打印数据之后查看数据类型，直到拿到字典类型
        let userInfo = NSDictionary(contentsOfFile: saveUserInfoPath)?["data"] as? NSDictionary
//        print(userInfo)
        //赋值
        //头像
        //头像是加密的  先解密
        let iconStr = userInfo?["studentphoto"] as? String
        let iconData = Data(base64Encoded: iconStr ?? "", options: Data.Base64DecodingOptions.ignoreUnknownCharacters)
        leftIconView.image = UIImage(data: iconData!)
        //姓名
        leftNameL.text = (userInfo?["studentName"] as? String ?? "") + "    校长"
        leftAccountL.text = userInfo?["studentNum"] as? String
        leftSchoolNameL.text = userInfo?["schoolname"] as? String
        leftScoreL.text = userInfo?["studentScore"] as? String
        //注释出字典对应的key信息
        /*
         data =     {
         address = "*";
         classid = 1071394;
         classname = "\U4e94(1)\U73ed";
         grade = "\U4e94\U5e74\U7ea7";
         id = 2;
         key = 2;
         qq = "*";
         schoolname = "\U9f99\U817e\U4e91\U6559\U80b2\U79d1\U6280";
         sex = "\U5973";
         studentName = "\U738b\U6d77\U5a77";
         studentNum = 5102;
         studentScore = 1;
         studentphoto =
         */
        
        let score = Int(leftScoreL.text!) ?? 0
        var scoreRank: Int = 0
        switch score {
        case 0..<10:
            scoreRank = 0
        case 10..<50:
            scoreRank = 1
        case 50..<150:
            scoreRank = 2
        case 150..<350:
            scoreRank = 3
        case 350..<850:
            scoreRank = 4
        default:
            scoreRank = 5 }
        //添加分数等级视图
        for i in 1..<6{
            //通过上面的表现分等级 要判断分数分级的颜色
            if i > scoreRank {
//                初始化图片视图
                imageV = UIImageView(image: UIImage(named: "jujube\(i)_dark"))
            }else{
              imageV = UIImageView(image: UIImage(named: "jujube\(i)_light"))
            }
            //添加到分数分级视图
            imageV.tag = 99+i
            leftScoreBgView.addSubview(imageV)
        }
        //在这里是打印不出frame的，因为在这里背景视图还没有加载出来
//        print("-----",leftScoreBgView.frame)
    }

    
    
    //视图的生命周期方法
    override func viewDidAppear(_ animated: Bool) {
       
        //获得背景视图的实际宽度
        let bgViewW = leftScoreBgView.frame.width
        let imageW = bgViewW / 5
        for iv in leftScoreBgView.subviews {
            // 根据背景图的宽度获得图片左边的距离
            let margin = imageW
            // 计算x中心值
            let x = margin * CGFloat(iv.tag - 100) + iv.bounds.width/2
            // 设置约束
//            iv.snp.makeConstraints({ (make) in
//                make.centerX.equalTo(leftScoreBgView.snp.left).offset(x)
//                make.bottom.equalTo(leftScoreBgView.snp.bottom).offset(-10)
//            })
           iv.snp.makeConstraints({ (make) in
            make.centerX.equalTo(leftScoreBgView.snp.left).offset(x)
            make.bottom.equalTo(leftScoreBgView.snp.bottom).offset(-10)
           })
        }
    }
}
